

cap file close Table
pause on 

local outcomes					$outcomes

local all_outcomes      		$`outcomes'

local title						`outcomes'_endline_1_merged_ctrls


* Estimate ITTs using Egypt endline 1 data 
				
	local count_out = 0 
	mat def pvalue = J(25,4,.)


	foreach outcome in `all_outcomes' {

	use  "Data/endline_1_egypt_fem.dta", clear 

		  local count_out = `count_out' + 1
			
			* Total ITT
			
				eststo total_itt: reghdfe `outcome' total_itt $controls $controls_Egypt_only, cluster(vil) absorb(matchid)
				
					local c_1_`outcome' : di%12.3f _b[total_itt]
					local se_1_`outcome' : di%12.3f _se[total_itt]
					local n_1_`outcome' = e(N)
					local r2_1_`outcome' = e(R2)
					
					local p_1_`outcome' : di%12.3f ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					mat def pvalue[`count_out',1] = ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					sum `outcome' if total_itt ==0	
					local m_1_`outcome' : di%12.3f `r(mean)'

					local c_m1_`outcome'  =  `c_1_`outcome''/`m_1_`outcome''
					local c_m1_`outcome' 	 : di%12.3f `c_m1_`outcome'' 
		
			* Within
				
				eststo within: reghdfe `outcome' within $controls $controls_Egypt_only, cluster(vil) absorb(matchid)
					
					local c_2_`outcome' : di%12.3f _b[within]
					local se_2_`outcome' : di%12.3f _se[within]
					local n_2_`outcome' = e(N)
					local r2_2_`outcome' = e(R2)
					local p_2_`outcome' : di%12.3f ttail(e(df_r),abs(_b[within]/_se[within]))*2
					
					mat def pvalue[`count_out',2] = ttail(e(df_r),abs(_b[within]/_se[within]))*2	
					
					sum `outcome' if within ==0	
					local m_2_`outcome' : di%12.3f `r(mean)'

					local c_m2_`outcome'  =  `c_2_`outcome''/`m_2_`outcome''
					local c_m2_`outcome' 	 : di%12.3f `c_m2_`outcome'' 
			
			* Between
			
				eststo between: reghdfe `outcome' between $controls $controls_Egypt_only, cluster(vil) absorb(matchid)
				
					local c_3_`outcome' : di%12.3f  _b[between]
					local se_3_`outcome' : di%12.3f _se[between]
					local n_3_`outcome'  = e(N)
					local r2_3_`outcome' = e(R2)
					local p_3_`outcome' : di%12.3f ttail(e(df_r),abs(_b[between]/_se[between]))*2
					
					mat def pvalue[`count_out',3] = ttail(e(df_r),abs(_b[between]/_se[between]))*2	
					
					sum `outcome' if between ==0	
					local m_3_`outcome' : di%12.3f `r(mean)'

					local c_m3_`outcome'  =  `c_3_`outcome''/`m_3_`outcome''
					local c_m3_`outcome' 	 : di%12.3f `c_m3_`outcome'' 

					
			* Spillover  
			
				eststo spill: reghdfe `outcome' spillovers $controls $controls_Egypt_only, cluster(vil) absorb(matchid)
				
					local c_4_`outcome' : di%12.3f _b[spillovers]
					local se_4_`outcome' : di%12.3f _se[spillovers]
					local n_4_`outcome' = e(N)
					local r2_4_`outcome' = e(R2)
					local p_4_`outcome' : di%12.3f ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					sum `outcome' if spillovers ==0	
					local m_4_`outcome' : di%12.3f `r(mean)'

					local c_m4_`outcome'  =  `c_4_`outcome''/`m_4_`outcome''
					local c_m4_`outcome' 	 : di%12.3f `c_m4_`outcome'' 

			* GE  
			
				eststo infrastructure: reghdfe `outcome' infrastructure $controls $controls_Egypt_only, cluster(vil) absorb(matchid)
				
					local c_9_`outcome' : di%12.3f _b[infrastructure]
					local se_9_`outcome' : di%12.3f _se[infrastructure]
					local n_9_`outcome' = e(N)
					local r2_9_`outcome' = e(R2)
					local p_9_`outcome' : di%12.3f ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					sum `outcome' if infrastructure ==0	
					local m_9_`outcome' : di%12.3f `r(mean)'

					local c_m9_`outcome'  =  `c_9_`outcome''/`m_9_`outcome''
					local c_m9_`outcome' 	 : di%12.3f `c_m9_`outcome'' 
					
	}
				
				
			
* Store significance level based on p-value
	
	foreach outcome of local all_outcomes {
		
		local s_1_`outcome' ""
		local s_2_`outcome' ""
		local s_3_`outcome' ""
		local s_4_`outcome' ""
		local s_9_`outcome' ""
		
		* Total ITT 
		
			if `p_1_`outcome'' < 0.1{
				local s_1_`outcome' "*"
			}
			if `p_1_`outcome'' < 0.05{
				local s_1_`outcome' "**"
			}
			if `p_1_`outcome'' < 0.01{
				local s_1_`outcome' "***"
			}
			
		* Within
		
			if `p_2_`outcome'' < 0.1{
				local s_2_`outcome' "*"
			}
			if `p_2_`outcome'' < 0.05{
				local s_2_`outcome' "**"
			}
			if `p_2_`outcome'' < 0.01{
				local s_2_`outcome' "***"
			}
			
		* Between 
		
			if `p_3_`outcome'' < 0.1{
				local s_3_`outcome' "*"
			}
			if `p_3_`outcome'' < 0.05{
				local s_3_`outcome' "**"
			}
			if `p_3_`outcome'' < 0.01{
				local s_3_`outcome' "***"
			}
		
		* Spillover 
		
			if `p_4_`outcome'' < 0.1{
				local s_4_`outcome' "*"
			}
			if `p_4_`outcome'' < 0.05{
				local s_4_`outcome' "**"
			}
			if `p_4_`outcome'' < 0.01{
				local s_4_`outcome' "***"
			}
	
	    * GE 
		
			if `p_9_`outcome'' < 0.1{
				local s_9_`outcome' "*"
			}
			if `p_9_`outcome'' < 0.05{
				local s_9_`outcome' "**"
			}
			if `p_9_`outcome'' < 0.01{
				local s_9_`outcome' "***"
			}
	
	}
	
		
	
* Estimate ITTs using Tunisia endline 1 data 

	local count_out = 0 
	mat def pvalue = J(25,4,.)


	foreach outcome in `all_outcomes' {

	use  "Data/endline_1_tunisia_fem.dta", clear 

		   local count_out = `count_out' + 1
					
			
			* Total ITT
			
				eststo total_itt: regress `outcome' total_itt $controls $controls_Tunisia_only i.Strata, vce (cluster imada)
				
					local c_5_`outcome' : di%12.3f _b[total_itt]
					local se_5_`outcome' : di%12.3f _se[total_itt]
					local n_5_`outcome' = e(N)
					local r2_5_`outcome' = e(R2)
					
					local p_5_`outcome' : di%12.3f ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					mat def pvalue[`count_out',1] = ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					sum `outcome' if total_itt ==0	
					local m_5_`outcome' : di%12.3f `r(mean)'

					local c_m5_`outcome'  =  `c_5_`outcome''/`m_5_`outcome''
					local c_m5_`outcome' 	 : di%12.3f `c_m5_`outcome'' 
					
			* Within
			
				eststo within: regress `outcome' within $controls $controls_Tunisia_only i.Strata, vce (cluster imada)
				
					local c_6_`outcome' : di%12.3f  _b[within]
					local se_6_`outcome' : di%12.3f _se[within]
					local n_6_`outcome'  = e(N)
					local r2_6_`outcome' = e(R2)
					local p_6_`outcome' : di%12.3f ttail(e(df_r),abs(_b[within]/_se[within]))*2
					
					mat def pvalue[`count_out',2] = ttail(e(df_r),abs(_b[within]/_se[within]))*2	
					
					sum `outcome' if within ==0	
					local m_6_`outcome' : di%12.3f `r(mean)'

					local c_m6_`outcome'  =  `c_6_`outcome''/`m_6_`outcome''
					local c_m6_`outcome' 	 : di%12.3f `c_m6_`outcome'' 
			
			* Between 
				
				eststo between: regress `outcome' between $controls $controls_Tunisia_only i.Strata, vce (cluster imada)
					
					local c_7_`outcome' : di%12.3f _b[between]
					local se_7_`outcome' : di%12.3f _se[between]
					local n_7_`outcome' = e(N)
					local r2_7_`outcome' = e(R2)
					local p_7_`outcome' : di%12.3f ttail(e(df_r),abs(_b[between]/_se[between]))*2
					
					mat def pvalue[`count_out',3] = ttail(e(df_r),abs(_b[between]/_se[between]))*2
					
					sum `outcome' if between ==0	
					local m_7_`outcome' : di%12.3f `r(mean)'

					local c_m7_`outcome'  =  `c_7_`outcome''/`m_7_`outcome''
					local c_m7_`outcome' 	 : di%12.3f `c_m7_`outcome'' 
			
			* Spillover  
			
				eststo spill: regress `outcome' spillovers $controls $controls_Tunisia_only i.Strata, vce (cluster imada)
				
					local c_8_`outcome' : di%12.3f _b[spillovers]
					local se_8_`outcome' : di%12.3f _se[spillovers]
					local n_8_`outcome' = e(N)
					local r2_8_`outcome' = e(R2)
					local p_8_`outcome' : di%12.3f ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					sum `outcome' if spillovers ==0	
					local m_8_`outcome' : di%12.3f `r(mean)'

					local c_m8_`outcome'  =  `c_8_`outcome''/`m_8_`outcome''
					local c_m8_`outcome' 	 : di%12.3f `c_m8_`outcome'' 
					
			* GE
			
				eststo infrastructure: regress `outcome' infrastructure $controls $controls_Tunisia_only i.Strata, vce (cluster imada)
				
					local c_0_`outcome' : di%12.3f _b[infrastructure]
					local se_0_`outcome' : di%12.3f _se[infrastructure]
					local n_0_`outcome' = e(N)
					local r2_0_`outcome' = e(R2)
					local p_0_`outcome' : di%12.3f ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					sum `outcome' if infrastructure ==0	
					local m_0_`outcome' : di%12.3f `r(mean)'

					local c_m0_`outcome'  =  `c_0_`outcome''/`m_0_`outcome''
					local c_m0_`outcome' 	 : di%12.3f `c_m0_`outcome'' 
			
				
	}
		


* Store significance level based on p-value (or adjusted p-value)
	
	foreach outcome of local all_outcomes {
		
		local s_5_`outcome' ""
		local s_6_`outcome' ""
		local s_7_`outcome' ""
		local s_8_`outcome' ""
		local s_0_`outcome' ""
		
		* Total ITT
		
			if `p_5_`outcome'' < 0.1{
				local s_5_`outcome' "*"
			}
			if `p_5_`outcome'' < 0.05{
				local s_5_`outcome' "**"
			}
			if `p_5_`outcome'' < 0.01{
				local s_5_`outcome' "***"
			}
		
		* Within
		
			if `p_6_`outcome'' < 0.1{
				local s_6_`outcome' "*"
			}
			if `p_6_`outcome'' < 0.05{
				local s_6_`outcome' "**"
			}
			if `p_6_`outcome'' < 0.01{
				local s_6_`outcome' "***"
			}
			
		* Between
		
			if `p_7_`outcome'' < 0.1{
				local s_7_`outcome' "*"
			}
			if `p_7_`outcome'' < 0.05{
				local s_7_`outcome' "**"
			}
			if `p_7_`outcome'' < 0.01{
				local s_7_`outcome' "***"
			}
			
		* Spillovers
		
			if `p_8_`outcome'' < 0.1{
				local s_8_`outcome' "*"
			}
			if `p_8_`outcome'' < 0.05{
				local s_8_`outcome' "**"
			}
			if `p_8_`outcome'' < 0.01{
				local s_8_`outcome' "***"
			}
			
		* Infrastructure
		
			if `p_0_`outcome'' < 0.1 {
				local s_0_`outcome' "*"
			}
			if `p_0_`outcome'' < 0.05{
				local s_0_`outcome' "**"
			}
			if `p_0_`outcome'' < 0.01{
				local s_0_`outcome' "***"
			}
	
	}
	
	foreach outcome of local all_outcomes {
		
		 local l_`outcome' : variable label `outcome'
		
	}

	
	
* Save tables to Dropbox

	file open Table using "Output/`title'.tex", text write replace

	file write Table  _n ///
	"\begin{tabular}{l*{13}{c}} \hline & \multicolumn{4}{c}{\textbf{Between}} & \multicolumn{4}{c}{\textbf{Within}} & \multicolumn{4}{c}{\textbf{Spillovers}} \\ \cmidrule(r){2-5}\cmidrule(l){6-9} \cmidrule(l){10-13} & $\beta$ & {SE} & {N} & \shortstack{Control\\mean} & $\beta$ & {SE} & {N} & \shortstack{Control\\mean}  & $\beta$ & {SE}  & {N} & \shortstack{Control\\mean} \\ \cmidrule(r){2-5}\cmidrule(l){6-9} \cmidrule(l){10-13} & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} & {(7)} & {(8)} & {(9)} & {(10)} & {(11)} & {(12)} \\ \midrule \multicolumn{1}{p{1.3cm}}{\textbf{Egypt}} & &  &    \\ \cmidrule(r){1-5} \cmidrule(l){6-9} \cmidrule(l){10-13}"   _n

	foreach outcome of local all_outcomes {
		
	file write Table																																	_n ///
	"`l_`outcome'' & `c_3_`outcome''`s_3_`outcome'' & `se_3_`outcome''   & `n_3_`outcome'' & `m_3_`outcome''   &  `c_2_`outcome''`s_2_`outcome'' & `se_2_`outcome'' & `n_2_`outcome'' & `m_2_`outcome'' & `c_4_`outcome''`s_4_`outcome'' & `se_4_`outcome''  & `n_4_`outcome'' &      `m_4_`outcome''                                                                                        \\"  _n 
	}

	file write Table  _n ///
	"\cmidrule(r){1-5} \cmidrule(l){6-9} \cmidrule(l){10-13} \multicolumn{1}{p{1.3cm}}{\textbf{Tunisia}} & &  &    \\ \cmidrule(r){1-5} \cmidrule(l){6-9} \cmidrule(l){10-13} "   _n


	foreach outcome of local all_outcomes {
		
		file write Table                   _n ///
	"`l_`outcome'' & `c_7_`outcome''`s_7_`outcome'' &`se_7_`outcome'' & `n_7_`outcome'' & `m_7_`outcome''  & `c_6_`outcome''`s_6_`outcome''  &`se_6_`outcome'' & `n_6_`outcome'' & `m_6_`outcome''    & `c_8_`outcome''`s_8_`outcome'' & `se_8_`outcome''  & `n_8_`outcome''  & `m_8_`outcome''                                                                                            \\"  _n 
	}

	file write Table					_n ///
			"\hline \end{tabular}"													_n

	file close Table



